package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.ProjectCollectListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.ProjectCollectOriginal;
import com.isunland.managesystem.entity.ProjectCollectOriginal.ProjectCollectContent;
import com.isunland.managesystem.entity.ProjectSearchContent;
import com.isunland.managesystem.utils.MyDateUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

public class ProjectCollectListFragment extends BaseListFragment {

  public static final String EXTRA_CONTENT =
      "com.isunland.managesystem.entity.ProjectCollectListFragment.EXTRA_CONTENT";

  private static final int SEARCH_COLLECT = 0;
  private static final int COUNT = 1;//统计
  private ArrayList<ProjectCollectContent> mlist;
  private ProjectCollectListAdapter mAdapter;

  private String customerId = "";
  private String customerName = "";

  //    private String propertyCode = "";//性质
  //    private String propertyName = "全部";

  private String projectState = "";
  private String projectStateName = "";//阶段名

  private String startDateBegin =
      MyDateUtil.dateToString(MyDateUtil.getBeforeYear(1), MyDateUtil.TYPE_DATE);//(开工日期 开始)
  private String startDateEnd = MyDateUtil.dateToString(new Date(), MyDateUtil.TYPE_DATE);
      //(开工日期 结束)
  //
  private String projectName = "";//项目名称

  //    private String requireDateBegin = "";//(开工日期 开始)
  //    private String requireDateEnd = "";//(开工日期 结束
  //
  //    private String ifOver = "";
  //    private String ifOverName = "全部";

  private String pmId = "";
  private String pmName = "";

  //    private String projectTypeCode = "";
  //    private String projectTypeName = "全部";

  private String projectKindName = "";//项目类别
  private String projectKindCode = "";

  //    private String salesId = "";
  //    private String salesName = "";

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    BaseVolleyActivity activity = (BaseVolleyActivity) getActivity();
    ActionBar actionBar;
    if (activity != null) {
      actionBar = activity.getSupportActionBar();
      if (actionBar != null) {
        if (NavUtils.getParentActivityName(activity) != null) {
          actionBar.setDisplayHomeAsUpEnabled(true);
        }
        actionBar.setTitle(R.string.projec_collect);
      }
    }
    super.onCreate(savedInstanceState);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_query_contract, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;
      //查询
      case R.id.menu_item_query:
        //                mSearchContent = new ProjectSearchContent(customerId, customerName, propertyCode, propertyName,
        //                        projectState, projectStateName, startDateBegin, startDateEnd, projectName,
        //                        requireDateBegin, requireDateEnd, ifOver, ifOverName, pmId, pmName, projectTypeCode, projectTypeName,
        //                        projectKindName, projectKindCode, salesId, salesName);

        ProjectSearchContent mSearchContent =
            new ProjectSearchContent(customerId, customerName, projectState, projectStateName,
                startDateBegin, startDateEnd, projectName, pmId, pmName, projectKindName,
                projectKindCode);
        Intent i = new Intent(getActivity(), SearchProjectCollectActivity.class);
        i.putExtra(SearchProjectCollectFragment.EXTRA_CONTENT, mSearchContent);
        startActivityForResult(i, SEARCH_COLLECT);
        break;
      //统计
      case R.id.menu_item_contract:
        //                mSearchContent = new ProjectSearchContent(customerId, customerName, propertyCode, propertyName,
        //                        projectState, projectStateName, startDateBegin, startDateEnd, projectName,
        //                        requireDateBegin, requireDateEnd, ifOver, ifOverName, pmId, pmName, projectTypeCode, projectTypeName,
        //                        projectKindName, projectKindCode, salesId, salesName);

        mSearchContent =
            new ProjectSearchContent(customerId, customerName, projectState, projectStateName,
                startDateBegin, startDateEnd, projectName, pmId, pmName, projectKindName,
                projectKindCode);
        Intent countIntent = new Intent(getActivity(), ProjectCountListActivity.class);
        countIntent.putExtra(ProjectCountListFragment.EXTRA_CONTENT, mSearchContent);
        startActivityForResult(countIntent, COUNT);
        break;

      default:
        break;
    }

    return super.onOptionsItemSelected(item);
  }

  @Override public String getUrl() {

    return ApiConst.ULR_PROJECT_COLLECT;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    //        params.put("customerId", customerId);
    //        params.put("memberCode", CurrentUser.newInstance(getActivity()).getMemberCode());
    //        params.put("dataStatus", "publish");
    //        params.put("startDateBegin", startDateBegin);
    //        params.put("startDateEnd", startDateEnd);
    //        params.put("requireDateBegin", requireDateBegin);
    //        params.put("requireDateEnd", requireDateEnd);
    //        params.put("projectState", projectState);
    //        params.put("ifOver", ifOver);
    //        params.put("projectPropertyCode", propertyCode);
    //        params.put("projectTypeCode", projectTypeCode);
    //        params.put("projectKindCode", projectKindCode);
    //        params.put("projectName", projectName);
    //        params.put("pmId", pmId);
    //        params.put("salesId", salesId);
    //        params.put("page", "1");
    //        params.put("rows", "1000");

    params.put("customerId", customerId);
    params.put("memberCode", CurrentUser.newInstance(getActivity()).getMemberCode());
    params.put("dataStatus", "publish");
    params.put("beginprojectSignDate", startDateBegin);
    params.put("endprojectSignDate", startDateEnd);
    params.put("projectState", projectState);
    params.put("projectKindCode", projectKindCode);
    params.put("projectName", projectName);
    params.put("pmId", pmId);
    params.put("page", "1");
    params.put("rows", "1000");
    return params;
  }

  @Override public void resolveResponse(String json) {
    ProjectCollectOriginal original = new Gson().fromJson(json, ProjectCollectOriginal.class);
    ArrayList<ProjectCollectContent> rows = (ArrayList<ProjectCollectContent>) original.getRows();
    if (mlist == null) {
      mlist = new ArrayList<>();
    }
    mlist.clear();
    mlist.addAll(rows);
    setAdapter(mlist);
  }

  private void setAdapter(ArrayList<ProjectCollectContent> mlist2) {
    if (mAdapter == null) {
      mAdapter = new ProjectCollectListAdapter(getActivity(), mlist2);
    }
    setListAdapter(mAdapter);
    ((ProjectCollectListAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    ProjectCollectContent content = mlist.get(position - 1);
    Intent i = new Intent(getActivity(), DetailProjectCollectActivity.class);
    i.putExtra(DetailProjectCollectFragment.EXTRA_CONTENT, content);
    startActivity(i);
    super.onListItemClick(l, v, position, id);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == SEARCH_COLLECT) {
      ProjectSearchContent content =
          (ProjectSearchContent) data.getSerializableExtra(EXTRA_CONTENT);
      updateParams(content);
      volleyPost();
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  private void updateParams(ProjectSearchContent content) {
    customerId = content.getCustomerId();
    customerName = content.getCustomerName();

    //        propertyCode = content.getPropertyCode();//性质
    //        propertyName = content.getPropertyName();

    projectState = content.getProjectState();//阶段
    projectStateName = content.getProjectStateName();//阶段名

    startDateBegin = content.getStartDateBegin();//(开工日期 开始)
    startDateEnd = content.getStartDateEnd();//(开工日期 结束)

    projectName = content.getProjectName();//项目名称

    //        requireDateBegin = content.getRequireDateBegin();//(开工日期 开始)
    //        requireDateEnd = content.getRequireDateEnd();//(开工日期 结束)
    //
    //        ifOver = content.getIfOver();
    //        ifOverName = content.getIfOverName();

    pmId = content.getPmId();
    pmName = content.getPmName();

    //        projectTypeCode = content.getProjectTypeCode();
    //        projectTypeName = content.getProjectTypeName();

    projectKindName = content.getProjectKindName();
    projectKindCode = content.getProjectKindCode();

    //        salesId = content.getSalesId();
    //        salesName = content.getSalesName();

  }
}
